package com.hx.vending.controller;

import com.hx.vending.annotation.AnnotationField;
import com.hx.vending.annotation.Authorization;
import com.hx.vending.annotation.CurrentFieldId;
import com.hx.vending.annotation.CurrentUserId;
import com.hx.vending.bean.Goods;
import com.hx.vending.service.*;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created by Administrator on 2018/11/26.
 * 商品
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService IGoodsService;

    /**
     * 插入商品信息
     * @param goods
     * @return
     */
    @RequestMapping(value = "/insertGoods", method = RequestMethod.POST)
    @ResponseBody
    @Authorization
    public Msg insertGoods(Goods goods, @CurrentUserId Integer userId){
        return this.IGoodsService.insertGoods(goods, userId);
    }


    /**
     * 根据用户id及其他条件查询商品
     * @param userId
     * @param goods
     * @return
     */
    @RequestMapping(value = "/selectGoodsByUserId", method = RequestMethod.GET)
    @ResponseBody
    @Authorization
    public Msg selectGoodsByUserId(@CurrentUserId Integer userId, Goods goods, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize){
        return this.IGoodsService.selectGoodsByUserId(userId, goods, pageNum, pageSize);
    }


    /**
     * 删除商品
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/deleteGoods/{goodsId}", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteGoods(@PathVariable Integer goodsId){
        return this.IGoodsService.deleteGoods(goodsId);
    }


    /**
     * 修改商品信息
     * @param goods
     * @return
     */
    @RequestMapping(value = "/updateGoods", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateGoods(Goods goods){
        return this.IGoodsService.updateGoods(goods);
    }


    /**
     * 购买商品结算(ANDROID and WEB)
     * @param equipmentId     设备id
     * @param trailsJsonStr   购物车信息 json   {货道号，数量}
     * @return
     */
    @PostMapping("/updateBuyCommodity")
    @ResponseBody
    public Msg updateBuyCommodity(String equipmentId, String trailsJsonStr){
        try {
            return this.IGoodsService.updateBuyCommodity(equipmentId, trailsJsonStr);
        } catch (Exception e) {
            System.out.println(e);
            return Msg.fail();
        }
    }


    /*
    * 商品统计
    * */
    @ResponseBody
    @RequestMapping("/getGoodsStatisticsByUser")
    @Authorization
    @AnnotationField
    public Msg getGoodsStatisticsByUser(@CurrentUserId Integer userId, @CurrentFieldId Integer[] integers, String startTime, String endTime, Integer fieldId,
                                        String condition, @RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum){
        return IGoodsService.getGoodsStatisticsByUser(startTime,integers,endTime,fieldId,userId,condition,pageNum);
    }

    /**
     *查询设备下商品消耗记录
     *
     * @param userId   用户ID
     * @return
     */
    @RequestMapping("/getGoodsByEquipmentInfo")
    @ResponseBody
    @Authorization
    public Msg getGoodsByEquipmentInfo(@CurrentUserId Integer userId,String startTime,String endTime, String equipmentId,
                                       @RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum){
        return IGoodsService.getGoodsByEquipment(userId,startTime,endTime,equipmentId,pageNum);
    }

    /*
    * 当前设备线上收益
    * */
    @RequestMapping("/getOnlineByEquipment")
    @ResponseBody
    @Authorization
    public Msg getOnlineByEquipment(@CurrentUserId Integer userId,String startTime,String endTime,String equipmentId,
                                    @RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum){
        return IGoodsService.getOnlineByEquipment(userId,startTime,endTime,equipmentId,pageNum);
    }
     /*
    * 当前设备线下收益
    * */
    @RequestMapping("/getUnderlineByEquipment")
    @ResponseBody
    @Authorization
    public Msg getUnderlineByEquipment(@CurrentUserId Integer userId,String startTime,String endTime,String equipmentId,
                                       @RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum){
        return IGoodsService.getUnderlineByEquipment(userId,startTime,endTime,equipmentId,pageNum);
    }

    /*
    * 商品管理
    * */
    @RequestMapping("/getGoodsByUser")
    @ResponseBody
    @Authorization
    public Msg getGoodsByUser(@CurrentUserId Integer userId,String condition){
        if(ToolUtil.isNotEmpty(userId) && userId != 0){
            Goods goods = new Goods();
            goods.setGoodsUserId(userId);
            if(ToolUtil.isNotEmpty(condition) && !condition.equals("") && !condition.equals(" ")){
                goods.setGoodsName(condition);
            }
            List<Goods> list = IGoodsService.selectByExample(goods);
            return Msg.success().add("goodsList",list);
        }else{
            return Msg.fail().add("va_msg", "未找到该商家信息");
        }
    }


    /**
     * 根据设备id查询商品
     * @param equipmentId
     * @return
     */
    @GetMapping("/selectGoodsByEquipmentId")
    @ResponseBody
    public Msg selectGoodsByEquipmentId(String equipmentId){
        return this.IGoodsService.selectGoodsByEquipmentId(equipmentId);
    }


    /**
     * 查询商品列表(无屏售货机购买商品)
     * @return
     */
    @RequestMapping("/selectShopCartGoodsByEquipmentId")
    @ResponseBody
    public Msg selectShopCartGoodsByEquipmentId(String equipmentId) {

        return this.IGoodsService.selectShopCartGoodsByEquipmentId(equipmentId);
    }


    /**
     * 特殊需求，购买结算商品前输入病区与床号
     * @param equipmentId              设备id
     * @param trailsJsonStr            商品jsonStr
     * @param areaNumber               病区
     * @param bedNumber                床号
     * @return
     */
    @RequestMapping("/insertMedicineOrder")
    @ResponseBody
    public Msg insertMedicineOrder (String equipmentId, String trailsJsonStr, String areaNumber, String bedNumber) {

        return this.IGoodsService.insertMedicineOrder(equipmentId, trailsJsonStr, areaNumber, bedNumber);
    }


    /**
     * 查询商品销售排行（不分设备，根据用户）
     * @param userId
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping("/selectGoodsRanking")
    @ResponseBody
    @Authorization
    public Msg selectGoodsRanking (@CurrentUserId Integer userId, String startTime, String endTime,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize) {

        return this.IGoodsService.selectGoodsRanking(userId, startTime, endTime, pageNum, pageSize);
    }

}
